<?php

class Journal2GoogleFonts {

    private $fonts = array();

    public function add($font_name, $font_subset, $font_weight) {
        if (!isset($this->fonts[md5($font_name)])) {
            $this->fonts[md5($font_name)] = array(
                'name'      => $font_name,
                'subset'    => array(),
                'weight'    => array()
            );
        }
        if (is_array($font_subset)) {
            foreach ($font_subset as $subset) {
                $this->fonts[md5($font_name)]['subset'][md5($subset)] = $subset;
            }
        } elseif ($font_subset) {
            $this->fonts[md5($font_name)]['subset'][md5($font_subset)] = $font_subset;
        }
        $this->fonts[md5($font_name)]['weight'][md5($font_weight)] = $font_weight;
    }

    public function getFonts() {
        $fonts = array();
        foreach ($this->fonts as $font) {
            $family = str_replace(' ', '+', $font['name']);
            $weight = $font['weight'] ? ':' . implode(',', $font['weight']) : '';
            $subset = $font['subset'] ? '&amp;subset=' . implode(',', $font['subset']) : '';
            $fonts[] = "//fonts.googleapis.com/css?family={$family}{$weight}{$subset}";
        }
        return $fonts;
    }
}